package com.sj.parksdk.net.http;

import com.sj.parksdk.net.base.CommonResult;
import com.sj.parksdk.net.manager.TokenManager;
import com.sj.parksdk.net.result.databean.Data_CarLicense;
import com.sj.parksdk.net.result.databean.Data_Ocr_ID;
import com.sj.parksdk.net.result.databean.Data_PackageOrder;
import com.sj.parksdk.net.result.databean.Data_ParkRecord;
import com.sj.parksdk.net.result.databean.Data_PrePayment;
import com.sj.parksdk.net.result.databean.Data_Token;
import com.sj.parksdk.net.result.databean.Data_VertifyCode;
import com.sj.parksdk.net.result.databean.Data_Weipay_Native;
import com.xuexiang.xhttp2.request.CustomRequest;
import com.xuexiang.xutil.common.StringUtils;

import java.io.File;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class ApiHelper {

    private static ApiHelper sInstance;

    private CustomRequest request;


    public static ApiHelper getInstance(CustomRequest request) {
        if (sInstance == null) {
            synchronized (ApiHelper.class) {
                if (sInstance == null) {
                    sInstance = new ApiHelper();
                }
            }
        }
        sInstance.request = request;
        return sInstance;
    }



    public Observable<CommonResult<Data_Token>> anonymous() {
        MyNetApi.Cetificat param = new MyNetApi.Cetificat();
        return request.create(MyRetorfitApi.TokenService.class).anonymous(param);
    }

    public Observable<CommonResult<Data_Token>> refreshToken() {
        MyNetApi.RefreshToken params = new MyNetApi.RefreshToken();
        params.setRefresh_token(TokenManager.getInstance().getRefreshToken());
        return request.create(MyRetorfitApi.TokenService.class).refresh(params);
    }


    public Observable<CommonResult> addCar(MyNetApi.CarAdd car) {
        return request.create(MyRetorfitApi.CarService.class).add(TokenManager.getInstance().getAuthorization(), car);
    }

    public Observable<CommonResult> deleteCar(long id) {
        return request.create(MyRetorfitApi.CarService.class).delete(MyNetApi.CAR_DELETE + id + "/delete", TokenManager.getInstance().getAuthorization());
    }

    public Observable<CommonResult> receiveCoupons(String code) {
        return request.create(MyRetorfitApi.CouponsService.class).receive(TokenManager.getInstance().getAuthorization(), new MyNetApi.CouponsReceive(code));
    }

    public Observable<CommonResult> loginSendMesg(String phone, boolean login) {
        MyNetApi.LoginPone param = new MyNetApi.LoginPone();
        param.setPhone(phone).setType(login ? "login" : "resetPassword ");
        return request.create(MyRetorfitApi.LoginService.class).send(param);
    }

    public Observable<CommonResult<Data_Token>> phoneLogin(String phone, String code) {
        MyNetApi.LoginPone param = new MyNetApi.LoginPone();
        param.setPhone(phone).setCode(code);
        return request.create(MyRetorfitApi.LoginService.class).phoneLogin(param);
    }

    public Observable<CommonResult<Data_VertifyCode>> vertifyCode(String phone, String code) {
        MyNetApi.LoginPone param = new MyNetApi.LoginPone();
        param.setPhone(phone).setCode(code).setType("resetPassword");
        return request.create(MyRetorfitApi.LoginService.class).verify(param);
    }

    public Observable<CommonResult> resetPassword(String paw, String token) {
        MyNetApi.UpdatePassword param = new MyNetApi.UpdatePassword();
        param.setPassword(paw).setToken(token);
        return request.create(MyRetorfitApi.LoginService.class).resetPassword(TokenManager.getInstance().getAuthorization(), param);
    }

    public Observable<CommonResult> logout() {
        MyNetApi.RefreshToken param = new MyNetApi.RefreshToken();
        param.setRefresh_token(TokenManager.getInstance().getRefreshToken());
        return request.create(MyRetorfitApi.LoginService.class).logout(TokenManager.getInstance().getAuthorization(), param);
    }

    public Observable<CommonResult<Data_Ocr_ID>> ocridcard(String type, File file) {
        String url = MyNetApi.OCR_IDCARD + "?type=" + type;
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        return request.create(MyRetorfitApi.OCRService.class).ocridcard(url, TokenManager.getInstance().getVisitAuthorization(), body);
    }

    public Observable<CommonResult<Data_CarLicense>> ocrvehiclelicense(File file) {
        String url = MyNetApi.OCR_CARLICENSE + "?type=face";
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        return request.create(MyRetorfitApi.OCRService.class).ocrwehiclelicense(url, TokenManager.getInstance().getVisitAuthorization(), body);
    }


    public Observable<CommonResult> userVerify(String name, String idNo, String startDate, String endDate) {
        MyNetApi.UserCertificate param = new MyNetApi.UserCertificate();
        param.setName(name).setIdNo(idNo).setStartDate(startDate).setEndDate(endDate);
        return request.create(MyRetorfitApi.VerifyService.class).idverify(TokenManager.getInstance().getAuthorization(), param);
    }

    public Observable<CommonResult> carVerify(long id, String carNo, String owner, String registerDate) {
        String url = MyNetApi.CAR_CERTIFICATE + id + "/verify";
        MyNetApi.CarCertificate param = new MyNetApi.CarCertificate();
        param.setId(id).setCarNo(carNo).setOwner(owner).setRegisterDate(registerDate);
        return request.create(MyRetorfitApi.VerifyService.class).carVerify(url, TokenManager.getInstance().getAuthorization(), param);
    }

    public Observable<CommonResult<Data_ParkRecord>> orderCalculatePrice(String orderNo, Long couponId) {
        String token = TokenManager.getInstance().getAuthorization();
        return request.create(MyRetorfitApi.OrderService.class).calculatePrice(StringUtils.isEmpty(token) ? TokenManager.getInstance().getVisitToken() : token, orderNo, couponId);
    }

    public Observable<CommonResult<Data_PackageOrder>> packageCreateOrder(long carId, long parkingLotId, long packageId, long packageItemId) {
        String token = TokenManager.getInstance().getAuthorization();
        MyNetApi.PackageCreateOrderBody body = new MyNetApi.PackageCreateOrderBody();
        body.setCarId(carId).setPackageId(packageId).setPackageItemId(packageItemId).setParkingLotId(parkingLotId);
        return request.create(MyRetorfitApi.OrderService.class).packageCreateOrder(token, body);
    }

    public Observable<CommonResult> payFreeTrade(String tradeNo, long timestamp,String sign) {
        String token = TokenManager.getInstance().getAuthorization();
        MyNetApi.Pay param = new MyNetApi.Pay();
        param.setTradeNo(tradeNo).setTimestamp(timestamp).setSign(sign);
        return request.create(MyRetorfitApi.PayService.class).payFreeTrade( token, param);
    }

    public Observable<CommonResult<Data_PrePayment>> prepayment(long parkingLotId, String carNo, long couponId) {
        String token = TokenManager.getInstance().getAuthorization();
        String visitToken = TokenManager.getInstance().getVisitToken();
        MyNetApi.PrePay param = new MyNetApi.PrePay();
        param.setCarNo(carNo).setParkingLotId(parkingLotId).setCouponId(couponId == 0?null:couponId);
        if(couponId==0)param.setCouponId(null);
        return request.create(MyRetorfitApi.PayService.class).prepayment( StringUtils.isEmpty(token)?visitToken:token, param);
    }

    public Observable<CommonResult<Data_Weipay_Native>> weipaynative(String tradeNo, long timestamp, String sign) {
        String token = TokenManager.getInstance().getAuthorization();
        String visitToken = TokenManager.getInstance().getVisitToken();
        MyNetApi.Pay  param = new MyNetApi.Pay();
        param.setTradeNo(tradeNo).setTimestamp(timestamp).setSign(sign);
        return request.create(MyRetorfitApi.PayService.class).weipaynative( StringUtils.isEmpty(token)?visitToken:token, param);
    }

}
